<?php

use App\Service\ConstantService;
use App\Service\RedisService;
use support\Db;
use Tinywan\Jwt\JwtToken;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Facades\JWTAuth;
use Webman\Http\Request;
use Webman\Http\Response;

/**
 * Here is your custom functions.
 */
function Y($data = '', $msg = '')
{
    $res = [
        'code' => 0,
        'data' => $data ?? '',
        'msg' => $msg ?? '',
    ];
    return json_encode($res);
}

function getRequestHeaders($param = null)
{
    $headers = array();
    foreach ($_SERVER as $name => $value) {
        if (substr($name, 0, 5) == 'HTTP_') {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
    if ($param != null) {
        return $headers[$param];
    }
    return $headers;
}

function process($request)
{

    $head = $request->header();
    $action = $request->action; // 当前操作的方法
    $noAuthArr = ['login', 'index', 'captcha']; // 在这个数组内的 方法 验证
    $code = 200;
    $msg = '';
    if (!in_array($action, $noAuthArr)) {
        $checkjwt = JwtToken::verify(1, $head['authorization']);
        $user = DB::table('wa_admins')->find($checkjwt['extend']['id']);
        $request->user_id = $checkjwt['extend']['id'];
        $request->user = $user;

        if (empty($checkjwt) || empty($checkjwt['extend']['id'])) {
            $code = 500;
            $msg = '登录验证失败';
        }
        if ($checkjwt['exp'] > time()){
//            return \app\controller\wmapi\IndexController::errored401($checkjwt['exp'],'token 过期');
//            return response('token 过期', 401);
            $code = 401;
            $msg = 'token 过期';
        }
        // return json(['errcode' => 0, 'errmsg' => 'ok','data'=>$action]);
    }
    $res = [
        'code' => $code,
        'msg'=> $msg
    ];
    return $res;
}

function adminId($request)
{

    $action = $request->action; // 当前操作的方法
    $noAuthArr = ['login', 'index', 'captcha']; // 在这个数组内的 方法 验证
    if (!in_array($action, $noAuthArr)) {
        $head = $request->header();
        $checkjwt = JwtToken::verify(1, $head['authorization']);
        $user = DB::table('wa_admins')->find($checkjwt['extend']['id']);
        $request->user_id = $checkjwt['extend']['id'];
        $request->user = $user;
    }else{
        $user = '';
    }

//        return $handler($request);
    return json_encode($user);
}


function dateTime(){
    return date('Y-m-d H:i:s', time());
}

function page($page){
    if (!$page){
        $page = 1;
    }else{
        $page = $page;
    }
    return $page;
}


function pageNum($pageNum){
    if (!$pageNum){
        $pageNum = 10;
    }else{
        $pageNum = $pageNum;
    }
    return $pageNum;
}

function between($start_time,$end_time){
    $between = [];
    if ($start_time && $end_time) {
        $between [] = ['created_at', '>', $start_time];
        $between [] = ['created_at', '<', $end_time];
    }

    return $between;
}


